<template>
  <el-dialog :visible.sync="_show"
             :title="title"
             append-to-body
             :close-on-click-modal="false"
             @close="onEventDialogClose">
    <k-form label-width="100px" :data="data" :rules="rules" @submit="onSubmit">

      <el-row>
        <el-col :span="12">
          <k-form-item label="用户名">
            <el-input v-model="data.username"/>
          </k-form-item>
        </el-col>

        <el-col :span="12">
          <k-form-item label="登陆密码">
            <el-input v-model="data.password" type="password" placeholder="如不修改请留空"/>
          </k-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <k-form-item label="姓名">
            <el-input v-model="data.realname"/>
          </k-form-item>
        </el-col>

        <el-col :span="12">
          <k-form-item label="邮箱">
            <el-input v-model="data.email"/>
          </k-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <k-form-item label="手机号">
            <el-input v-model="data.phone"/>
          </k-form-item>
        </el-col>

        <el-col :span="12">
          <k-form-item label="微信号">
            <el-input v-model="data.weixin"/>
          </k-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <k-form-item label="年龄">
            <el-input v-model="data.age"/>
          </k-form-item>
        </el-col>

        <el-col :span="12">
          <k-form-item label="性别">
            <el-radio-group v-model="data.sex">
              <el-radio :label="0">女</el-radio>
              <el-radio :label="1">男</el-radio>
            </el-radio-group>
          </k-form-item>
        </el-col>
      </el-row>

    </k-form>

    <div slot="footer">
      <el-button type="primary" @click="onSubmit" :loading="subing">保存</el-button>
      <el-button @click="_show = false">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'index-edit',
  props: {
    show: Boolean
  },
  computed: {
    _show: {
      get () {
        return this.show
      },
      set (v) {
        this.$emit('update:show', v)
      }
    },
    title () {
      return '修改用户信息'
    }
  },
  data () {
    return {
      data: {},
      rules: {},
      subing: false,
      successCallback: null
    }
  },
  methods: {
    onSubmit () {
      this.subing = true
      this.$http.post('admin/users', this.data)
        .finally(() => this.subing = false)
        .then((result) => {
          if (result.res_code === 0) {
            this.$message.success('保存成功')
            this._show = false
            this.$delete(result.data, 'password')
            this.successCallback(result.data)
          } else {
            this.$message.error(result.res_msg)
          }
        })
    },
    onEventDialogClose () {
      Object.assign(this.$data, this.$options.data())
    }
  }
};
</script>

<style scoped>

</style>
